﻿using Dapper;
using diagnostic_extensions_console.DiagnosticSource;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;

namespace diagnostic_extensions_console
{
    class Program
    {
        public const string ConnectionString =
        @"Server=localhost;Database=mytest;User ID=root;Password=123456;Trusted_Connection=False";
        static async Task Main(string[] args)
        {
            var observer = new DapperDiagnosticObserver();
            IDisposable subscription = DiagnosticListener.AllListeners.Subscribe(observer);

            Person result = await Get("1");
            Console.WriteLine($"{result.Name},{result.Age}");
        }

        public static async Task<Person> Get(string id)
        {
            using (IDbConnection conn = DapperService.GetSqlConnection(ConnectionString))
            {
                string sqlStr = @"SELECT remarks FROM tb_json where id = @id";
                var obj = await conn.QueryFirstOrDefaultAsync<string>(sqlStr,new { id = id});
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(obj);
            }
            
        }

        public class Person
        {
            public string Name { set; get; }
            public int Age { set; get; }
        }
    }
}
